import { createRouter, createWebHistory } from 'vue-router'
import LayOutView from '@/views/LayOutView.vue'
import LoginView from '@/views/LoginView.vue'
import OrderView from '@/views/OrderView.vue'
// import RightContentView from '@/views/RightContentView.vue'
import IndexView from '@/views/IndexView.vue'
import OrderEdit from '@/views/OrderEdit.vue'
import GoodsListView from '@/views/GoodsListView.vue'
import GoodsAddView from '@/views/GoodsAddView.vue'
import GoodsCategoryView from '@/views/GoodsCategoryView.vue'
import ShopView from '@/views/ShopView.vue'
import UserAddView from '@/views/UserAddView.vue'
import UserHomeView from '@/views/UserHomeView.vue'
import UserListView from '@/views/UserListView.vue'
import PassWordEditView from '@/views/PassWordEditView.vue'
import OrderTotalView from '@/views/OrderTotalView.vue'
import GoodsTotalView from '@/views/GoodsTotalView.vue'
import NotFoundView from '@/components/NotFoundView.vue'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      component: LoginView,
    },
    {
      path: '/index',
      redirect: '/index/main',
      meta: { roles: ['super', 'normal'] },
      component: LayOutView,
      children: [
        {
          path: '/index/main',
          name: '后台首页',
          meta: { roles: ['super', 'normal'] },
          component: IndexView,
        },
      ],
    },
    {
      path: '/order',
      redirect: '/index/order',
      meta: { roles: ['super', 'normal'] },
      component: LayOutView,
      children: [
        {
          path: '/index/order',
          name: '订单管理',
          meta: { roles: ['super', 'normal'] },
          component: OrderView,
        },

        {
          path: '/order/edit',
          name: '订单编辑',
          meta: { roles: ['super', 'normal'] },
          component: OrderEdit,
        },
      ],
    },
    {
      path: '/goods',
      redirect: '/goods/list',
      meta: { roles: ['super', 'normal'] },
      component: LayOutView,
      children: [
        {
          path: '/goods/list',
          name: '商品管理',
          component: GoodsListView,
          meta: { roles: ['super', 'normal'] },
        },
        {
          path: '/goods/add',
          name: '商品添加',
          meta: { roles: ['super', 'normal'] },
          component: GoodsAddView,
        },
        {
          path: '/goods/category',
          name: '商品分类',
          meta: { roles: ['super', 'normal'] },
          component: GoodsCategoryView,
        },
      ],
    },
    {
      path: '/shop',
      redirect: '/index/shop',
      meta: { roles: ['super'] },
      component: LayOutView,
      children: [
        {
          path: '/index/shop',
          name: '店铺管理',
          meta: { roles: ['super'] },
          component: ShopView,
        },
      ],
    },
    {
      path: '/user',
      redirect: '/user/list',
      meta: { roles: ['super', 'normal'] },
      component: LayOutView,
      children: [
        {
          path: '/user/list',
          name: '账号管理',
          meta: { roles: ['super', 'normal'] },
          component: UserListView,
        },
        {
          path: '/user/add',
          name: '账号添加',
          meta: { roles: ['super'] },
          component: UserAddView,
        },
        {
          path: '/user/edit',
          name: '密码修改',
          meta: { roles: ['super', 'normal'] },
          component: PassWordEditView,
        },
        {
          path: '/user/home',
          name: '个人中心',
          meta: { roles: ['super', 'normal'] },
          component: UserHomeView,
        },
      ],
    },
    {
      path: '/total',
      redirect: '/order/total',
      meta: { roles: ['super'] },
      component: LayOutView,
      children: [
        {
          path: '/order/total',
          name: '商品统计',
          meta: { roles: ['super'] },
          component: OrderTotalView,
        },
        {
          path: '/goods/total',
          name: '订单统计',
          meta: { roles: ['super'] },
          component: GoodsTotalView,
        },
      ],
    },
    {
      path: '/:pathMatch(.*)', // 捕获所有未匹配的路由
      name: 'NotFound',
      component: NotFoundView,
    },
  ],
})

const dynamicRoutes = [
  {
    path: '/order',
    redirect: '/index/order',
    meta: { roles: ['super', 'normal'] },
    component: LayOutView,
    children: [
      {
        path: '/index/order',
        name: '订单管理',
        meta: { roles: ['super', 'normal'] },
        component: OrderView,
      },

      {
        path: '/order/edit',
        name: '订单编辑',
        meta: { roles: ['super', 'normal'] },
        component: OrderEdit,
      },
    ],
  },
  {
    path: '/goods',
    redirect: '/goods/list',
    meta: { roles: ['super', 'normal'] },
    component: LayOutView,
    children: [
      {
        path: '/goods/list',
        name: '商品管理',
        component: GoodsListView,
        meta: { roles: ['super', 'normal'] },
      },
      {
        path: '/goods/add',
        name: '商品添加',
        meta: { roles: ['super', 'normal'] },
        component: GoodsAddView,
      },
      {
        path: '/goods/category',
        name: '商品分类',
        meta: { roles: ['super', 'normal'] },
        component: GoodsCategoryView,
      },
    ],
  },
  {
    path: '/shop',
    redirect: '/index/shop',
    meta: { roles: ['super'] },
    component: LayOutView,
    children: [
      {
        path: '/index/shop',
        name: '店铺管理',
        meta: { roles: ['super'] },
        component: ShopView,
      },
    ],
  },
  {
    path: '/user',
    redirect: '/user/list',
    meta: { roles: ['super', 'normal'] },
    component: LayOutView,
    children: [
      {
        path: '/user/list',
        name: '账号管理',
        meta: { roles: ['super', 'normal'] },
        component: UserListView,
      },
      {
        path: '/user/add',
        name: '账号添加',
        meta: { roles: ['super'] },
        component: UserAddView,
      },
      {
        path: '/user/edit',
        name: '密码修改',
        meta: { roles: ['super', 'normal'] },
        component: PassWordEditView,
      },
      {
        path: '/user/home',
        name: '个人中心',
        meta: { roles: ['super', 'normal'] },
        component: UserHomeView,
      },
    ],
  },
  {
    path: '/total',
    redirect: '/order/total',
    meta: { roles: ['super'] },
    component: LayOutView,
    children: [
      {
        path: '/order/total',
        name: '商品统计',
        meta: { roles: ['super'] },
        component: OrderTotalView,
      },
      {
        path: '/goods/total',
        name: '订单统计',
        meta: { roles: ['super'] },
        component: GoodsTotalView,
      },
    ],
  },
]

function addRot(role) {
  const accessRoutes = dynamicRoutes.filter((item) => {
    if (item.meta?.roles) {
      if (item.meta.roles.includes(role)) {
        if (item.children) {
          item.children = item.children.filter((item) => {
            if (item.meta?.roles) {
              if (item.meta.roles.includes(role)) {
                return true
              } else {
                return false
              }
            } else {
              return true
            }
          })
        }
        return true
      } else {
        return false
      }
    } else {
      return true
    }
  })

  console.log('🚀 ~ accessRoutes ~ accessRoutes:', accessRoutes)
  accessRoutes?.forEach((route) => {
    router.addRoute(route)
  })
}

router.beforeEach((to, from, next) => {
  const user = JSON.parse(localStorage.getItem('user'))
  if (user && user.token) {
    // addRot(user.role)
    if (to.path === '/') {
      next({ path: '/index/main' }) // 用户已登录，且尝试访问登录页面，重定向到主页
    } else if (user.role === 'normal') {
      if (to.path === '/order/total' || to.path === '/goods/total' || to.path === '/shop') {
        next({ path: '/NotFount' })
      } else {
        next()
      }
    } else {
      next() // 用户已登录，继续访问其他页面
    }
    next()
  } else {
    if (to.path === '/') {
      next() // 用户未登录，且正在访问登录页面，允许访问
    } else {
      next('/') // 用户未登录，且尝试访问非登录页面，重定向到登录页面
    }
  }
})

export default router
